package com.lions.client.controller;

import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.lions.client.model.Model;
import com.lions.client.view.buttonpanel.ButtonPanel;

public class Speech extends VerticalPanel {

	public Speech(ButtonPanel b) {
		super();
		buttons = b;

		getElement().setAttribute("x-webkit-speech", "SpeechCommands");
		setStyleName("voicepanel");
		
		voice = new TextBox();
		voice.getElement().setAttribute("x-webkit-speech", "SpeechCommands");
		voice.setStyleName("microphone");
		
		voiceInput = new Label();
		voiceInput.setStyleName("micinput");
		
		
		add(voice);
		add(voiceInput);
		
		voice.addFocusHandler(new FocusHandler(){
			@Override
			public void onFocus(FocusEvent event) {
				if (voice.getText() != null) {
					if (voice.getText().equals("add module")) {
						buttons.getAddButton().fireEvent(new ButtonClickEvent());
					} else if (voice.getText().equals("edit module")) {
						buttons.getEditButton().fireEvent(new ButtonClickEvent());
					} else if (voice.getText().equals("remove module")) {
						buttons.getModuleRemoveButton().fireEvent(new ButtonClickEvent());
					} 
					voiceInput.setText(voice.getText());
					voice.setText("");
				}
			}
		});
//		RootPanel.get().addDomHandler(new KeyPressHandler() {
//			@Override
//			public void onKeyPress(KeyPressEvent event) {
//				if (event.getCharCode() == '`') {
//					voice.setFocus(true);
//					
//				}
//			}
//		}, KeyPressEvent.getType());
	}
	
	public void addVoiceHandler(Button b, String s){
		button = b;
		newVoiceCommand = s;
		voice.addFocusHandler(new FocusHandler(){
			@Override
			public void onFocus(FocusEvent event) {
				if (voice.getText() != null) {
					System.out.println(voice.getText());
					if (voice.getText().equals(newVoiceCommand)) {
						button.fireEvent(new ButtonClickEvent());
					}
					voice.setText("");
				}
			}
		});
	}

	private Button button;
	private String newVoiceCommand;
	private ButtonPanel buttons;
	private TextBox voice;
	private Label voiceInput;
}
